package mumethod;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import mudbconnection.DBStudents;
import muobject.Student;

@Component("AccessStudentsService")
public class AccessStudentsService implements AccessStudents {

	@Autowired
	private DBStudents dbStudents;
	private List<Student> students;
	private List<String> years;
	private List<String> grades;
	private List<String> sclasses;
	private DateControl getDate;

	@SuppressWarnings("rawtypes")
	private List rows;

	public AccessStudentsService() {
		getDate = new LibFunction();
	}

	public List<Student> getAllStudents() {
		students = dbStudents.getAllStudents();
		return students;
	}

	public List<Student> getSpicalYearStudents(String name, String value, String sno, String year) {
		students = dbStudents.getSpicalStudents(name, value, sno, year);
		return students;
	}

	public List<Student> getSpicalStudents(String sno, String year,
			String grade, String sclass) {
		students = dbStudents.getSpicalYearStudents(sno, year, grade, sclass);
		return students;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<String> getAllYear(String sno) {
		rows = dbStudents.getAllYear(sno);
		years = new ArrayList();
		Iterator it = rows.iterator();
		while (it.hasNext()) {
			Map year = (Map) it.next();
			if (year.get("Years") != null)
				years.add(year.get("Years").toString());
		}
		return years;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<String> getAllGrade(String sno, String year, String sclass) {
		rows = dbStudents.getAllGrade(sno, year, sclass);
		grades = new ArrayList();
		Iterator it = rows.iterator();
		while (it.hasNext()) {
			Map grade = (Map) it.next();
			if (grade.get("Grade") != null)
				grades.add(grade.get("Grade").toString());
		}
		//grades.add("All");
		return grades;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<String> getAllSclass(String sno, String year, String grade) {
		rows = dbStudents.getAllSclass(sno, year, grade);
		sclasses = new ArrayList();
		Iterator it = rows.iterator();
		while (it.hasNext()) {
			Map sclass = (Map) it.next();
			if (sclass.get("Sclass") != null)
				sclasses.add(sclass.get("Sclass").toString());
		}
		//sclasses.add("All");
		return sclasses;
	}

	public boolean existStudent(String stno, String sno, String year) {
		return !(dbStudents.getSpicalStudents("Stno", stno, sno, year).isEmpty());
	}

	public boolean setStudentFromTable(Table table, String yeararg,
			String snoarg) {
		String stno, sno, grade, sclass, ename, cname, sex, year, password, stid;
		int classno;
		Date createdate;
		Date updatedate;
		Date birthday;
		try {
			for (TableItem item : table.getItems()) {
				stno = item.getText(0);
				sno = snoarg;
				grade = item.getText(1);
				sclass = item.getText(2);
				classno = Integer.valueOf(item.getText(3));
				cname = item.getText(4);
				ename = item.getText(5);
				sex = item.getText(6);
				birthday = Date.valueOf(item.getText(7));
				stid = item.getText(8);
				year = yeararg;
				createdate = new Date(getDate.getCurrentDateLong());
				updatedate = new Date(getDate.getCurrentDateLong());
				password = "";
				
				Student student = new Student(0 , stno, sno, grade, sclass,
						classno, ename, cname, sex, birthday, year, password,
						createdate, updatedate);
				if (!existStudent(stno, sno, year))
					dbStudents.setStudent(student);
				else
					dbStudents.updateStudent(student);
			}
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public boolean deleteStudentFromTable(Table table, String yeararg,
			String snoarg) {
		String stno, sno, grade, sclass, ename, cname, sex, year, password, stid;
		int classno;
		Date createdate;
		Date updatedate;
		Date birthday;
		try {
			for (TableItem item : table.getItems()) {
				stno = item.getText(0);
				sno = "";
				grade = item.getText(1);
				sclass = item.getText(2);
				classno = Integer.valueOf(item.getText(3));
				cname = item.getText(4);
				ename = item.getText(5);
				sex = item.getText(6);
				birthday = Date.valueOf(item.getText(7));
				stid = item.getText(8);
				year = yeararg;
				createdate = new Date(getDate.getCurrentDateLong());
				updatedate = new Date(getDate.getCurrentDateLong());
				password = "";
				
				Student student = new Student(Integer.valueOf(stid), stno, sno, grade, sclass,
						classno, ename, cname, sex, birthday, year, password,
						createdate, updatedate);
				if (existStudent(stno, sno, year))
					dbStudents.deleteStudentByStno(stno);
			}
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public Boolean deleteStudentByStno(String stno){
		try{
			dbStudents.deleteStudentByStno(stno);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public Boolean checkStudentTable(Table table){
		Boolean valid = true;
		for(TableItem item : table.getItems()){
			try{
				Integer.valueOf(item.getText(3));
				item.setBackground(3, new Color(table.getDisplay(),255,255,255));
			}catch(Exception e){
				item.setBackground(3, new Color(table.getDisplay(),255,48,48));
				valid = false;
			}
			try{
				if (!(item.getText(6).toUpperCase().equals("M") || item.getText(6).toUpperCase().equals("F")))
					throw new Exception("Sex Error!");
				item.setBackground(6, new Color(table.getDisplay(),255,255,255));
			}catch(Exception e){
				item.setBackground(6, new Color(table.getDisplay(),255,48,48));
				valid = false;
			}
			try{
				Date.valueOf(item.getText(7));
				item.setBackground(7, new Color(table.getDisplay(),255,255,255));
			}catch(Exception e){
				item.setBackground(7, new Color(table.getDisplay(),255,48,48));
				valid = false;
			}
		}
		return valid;
	}
	
	public List<String> getGradeByListYear(String sno, List<String> years){
		Set<Object> setGrades = new TreeSet<Object>();
		List<String> listGrades = new ArrayList<String>();
		for(String yearstr : years){
			for(String classstr : getAllGrade(sno, yearstr, "%")){
				setGrades.add(classstr);
			}
		}
		for (Object object : setGrades) {
			listGrades.add(object != null ? object.toString() : null);
		}
		return listGrades;
	}
	
	public List<String> getClassByListYearGrade(String sno, List<String> years, List<String> grades){
		Set<Object> setClasses = new TreeSet<Object>();
		List<String> listClasses = new ArrayList<String>();
		for(String yearstr : years){
			for(String gradestr : grades){
				for(String classstr : getAllSclass(sno, yearstr, gradestr)){
					setClasses.add(classstr);
				}
			}
		}
		for (Object object : setClasses) {
			listClasses.add(object != null ? object.toString() : null);
		}
		return listClasses;
	}
	
	public Map<String, String> getStudentStnoByListYearGradeClass(String sno, List<String> years, List<String> grades, List<String> classes){
		Set<Object> setStudents = new TreeSet<Object>();
		Map<String, String> mapStudent = new TreeMap();
		List<String> listStudents = new ArrayList<String>();
		for(String yearstr : years){
			for(String gradestr : grades){
				for(String classstr : classes){
					for(Student student : getSpicalStudents(sno, yearstr, gradestr, classstr)){
						String a = mapStudent.get(student.getStno());
						if(mapStudent.get(student.getStno()) == null)
						mapStudent.put(student.getStno(), getStudentsName(sno, student.getStno(), yearstr));
					}
				}
			}
		}
		return mapStudent;
	}
	
	public String getStudentsName(String sno, String stno, String year) {
		students = dbStudents.getSpicalStudents("Stno", stno, sno, year);
		try{
			return students.get(0).getCname();
		}catch(Exception e){
			return "";
		}
		
	}
	
}
